package com.dnw.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.DisplayMetrics;
import android.widget.Toast;

import com.dnw.shyfunny.BuildConfig;
import com.dnw.shyfunny.MainActivity;
import com.dnw.util.SharedPreferencesMgr;
import com.socks.library.KLog;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import org.litepal.LitePalApplication;

import java.util.Map;


/**
 * @author ChayChan
 * @description: Application类
 * @date 2017/6/10  15:44
 */

public class MyApp extends BaseApp {
    private MainActivity mMainActivity;
    private static MyApp instance;
    // 当前屏幕的高宽
    public int screenW = 0;
    public int screenH = 0;
    // 以键值对的形式存储用户名和密码
    public SharedPreferences sharereferences, shareStartBanner;

    public MainActivity getmMainActivity() {
        return mMainActivity;
    }

    public void setmMainActivity(MainActivity mMainActivity) {
        this.mMainActivity = mMainActivity;
    }

    public static MyApp getInstance() {
        if(null == instance){
            return new MyApp();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferencesMgr.init(this, "shyfunny");
        //**************************************相关第三方SDK的初始化等操作*************************************************
        KLog.init(BuildConfig.DEBUG);//初始化KLog
        LitePalApplication.initialize(getApplicationContext());//初始化litePal

        // 得到屏幕的宽度和高度
        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenW = dm.widthPixels;
        screenH = dm.heightPixels;

        MobclickAgent.setScenarioType(getContext(), MobclickAgent.EScenarioType.E_UM_NORMAL);

        PushAgent mPushAgent = PushAgent.getInstance(this);
//注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                System.out.println(deviceToken + "__________________________________________");
                //注册成功会返回device token
            }

            @Override
            public void onFailure(String s, String s1) {
                System.out.println(s + "__________________________________________" + s1);
            }
        });

        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                if (msg.extra != null) {
                    for (Map.Entry<String, String> entry : msg.extra.entrySet()) {
                        if (entry.getKey().equalsIgnoreCase("id")) {
                            SharedPreferencesMgr.setString(ApiConstant.SharedPreferencesEnum.PUSH_SHY_ID.name(), entry.getValue());
                            if (mMainActivity != null) {
                                mMainActivity.refresh();
                            } else {
                                Intent intent = new Intent();
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setClass(context, MainActivity.class);
                                startActivity(intent);
                            }
                        }
                    }
                }
            }
        };
        mPushAgent.setNotificationClickHandler(notificationClickHandler);

    }


}
